<?php

namespace XiaoZhuBundle\Controller\Api;

use AdminsBundle\Entity\Address;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use XiaoZhuBundle\Entity\UserAddress;
use XiaoZhuBundle\Helper\MyController;
/**
 * @Route("/Api")
 */
class AddressController extends MyController
{

    private $uid;
    public function __construct()
    {
        $this->uid = 1;
    }


    /**
     * @Route("/Address/My", name="ApiMyAddress")
     */
    public function ApiMyAddress(Request $request)
    {
        if (!$p= $this->getParamRequired("p",$request)) {$p=1;}
        $rep = $this->DB("XiaoZhuBundle:UserAddress");
        $address = $rep->findBy(array("uid"=>$this->uid),array("flag"=>"DESC"),10,($p-1)*10);
        $addresses = array();
        foreach ($address as $addr) {
            $addresses[] = array(
                "id"            => $addr->getId(),
                "name"          => $addr->getName(),
                "phone"         => $addr->getPhone(),
                "location"      => $addr->getLocation(),
            );
        }
        return $this->json_out(1,"列表获取成功",$addresses);
    }


    /**
     * @Route("/Address/Add", name="ApiAddAddress")
     */
    public function ApiAddAddress(Request $request)
    {
        if (!$name = $this->getParamRequired("name",$request)) {return $this->json_out(0,"参数缺失","name");}
        if (!$phone = $this->getParamRequired("phone",$request)) {return $this->json_out(0,"参数缺失","phone");}
        if (!$location = $this->getParamRequired("location",$request)) {return $this->json_out(0,"参数缺失","location");}

        $address = New UserAddress();
        $address->setUid($this->uid)
            ->setLocation($location)
            ->setName($name)
            ->setPhone($phone);
        $em = $this->EM();
        $em->persist($address);
        $em->flush();

        return $this->json_out(1,"地址添加成功");
    }

    /**
     * @Route("/Address/Edit", name="ApiEditAddress")
     */
    public function ApiEditAddress(Request $request)
    {

        if (!$id = $this->getParamRequired("id",$request)) {return $this->json_out(0,"参数缺失","id");}
        if (!$name = $this->getParamRequired("name",$request)) {return $this->json_out(0,"参数缺失","name");}
        if (!$phone = $this->getParamRequired("phone",$request)) {return $this->json_out(0,"参数缺失","phone");}
        if (!$location = $this->getParamRequired("location",$request)) {return $this->json_out(0,"参数缺失","location");}

        $rep = $this->DB("XiaoZhuBundle:UserAddress");
        $address = $rep->find($id);
        if (!$address) {
            return $this->json_out(0,"目标未找到");
        }
        $address->setUid($this->uid)
            ->setLocation($location)
            ->setName($name)
            ->setPhone($phone);
        $em = $this->getDoctrine()->getManager();
        $em->persist($address);
        $em->flush();

        return $this->json_out(1,"地址修改成功");
    }

    /**
     * @Route("/Address/Del", name="ApiDelAddress")
     */
    public function ApiDelAddress(Request $request)
    {

        if (!$id = $this->getParamRequired("id",$request)) {return $this->json_out(0,"参数缺失","id");}

        $rep = $this->DB("XiaoZhuBundle:UserAddress");
        $address = $rep->find($id);
        if (!$address) {
            return $this->json_out(0,"目标未找到");
        }
        $em = $this->getDoctrine()->getManager();
        $em->remove($address);
        $em->flush();

        return $this->json_out(1,"地址删除成功");
    }

    /**
     * @Route("/Address/SetDefault", name="ApiSetDefaultAddress")
     */
    public function ApiSetDefaultAddress(Request $request)
    {

        if (!$id = $this->getParamRequired("id",$request)) {return $this->json_out(0,"参数缺失","id");}

        $rep = $this->DB("XiaoZhuBundle:UserAddress");
        $address = $rep->find($id);
        if (!$address) {
            return $this->json_out(0,"目标未找到");
        }
        $em = $this->getDoctrine()->getManager();
        $address->setFlag(time());
        $em->persist($address);
        $em->flush();

        return $this->json_out(1,"默认地址设置成功");
    }



}
